const { merge } = require('webpack-merge')
const common = require('./webpack.common')
const path = require('path')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin')
const HtmlMinimizerPlugin = require('html-minimizer-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
/**
 * @type {import('webpack').Configuration}
 */
module.exports = merge(common, {
    mode: 'production',
    devtool: false,
    // 配置对应的 minimize 压缩要发布到生产环境的文件
    optimization: {
        minimize: true,
        minimizer: [
            // 压缩 css 文件
            new CssMinimizerPlugin(),
            // 压缩 js 文件
            new UglifyJsPlugin(),
            // 开启 gzip 压缩
            new CompressionPlugin({
                filename: '[path][base].gz',
                algorithm: 'gzip',
                test: /\.js$|\.css$|\.html$/,
                threshold: 10240,
                minRatio: 0.8
            })
        ]
    },
    plugins: [
        new CleanWebpackPlugin(),
        new CopyWebpackPlugin({
            patterns: [
                path.join(__dirname, 'public/favicon.ico')
            ]
        }),
        // 暂时忽略 warn
        new ImageMinimizerPlugin({
            test: /\.(jpe?g|png|gif|svg)$/i,
        }),
        new HtmlMinimizerPlugin()
    ]
})